package com.zzxz.practicaltrainingsys.controller;

import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Student;
import com.zzxz.practicaltrainingsys.vo.ViewStudent;
import com.zzxz.practicaltrainingsys.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Api(tags = "学生接口端")
@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    // 学生列表
    //http://localhost:8080/student/list
    @GetMapping("/list")
    public List<Student> getStudentList() {
        return studentService.getStudent();
    }

    // 查学生姓名
    //http://localhost:8080/student/names
    @GetMapping("/names")
    public List<String> getStudentNames() {
        return studentService.getStudentNames();
    }

    // 分页查询学生多表信息
    //http://localhost:8080/student/pages
    @ApiOperation("分页查询学生多表信息")
    @GetMapping("/pages")
    public PageInfo<ViewStudent> getPagesStudent(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "6") Integer pageSize,
            @RequestParam(required = false) Integer school_id,
            @RequestParam(required = false) Integer college_id,
            @RequestParam(required = false) Integer major_id,
            @RequestParam(required = false) Integer grade_id
    ) {
        PageInfo<ViewStudent> pageInfo = studentService.findStudentPages(pageNum, pageSize, school_id, college_id, major_id, grade_id);
        // 前端需要账号状态，后端直接返回student_state字段即可，无需过滤
        return pageInfo;
    }

    // 更新学生密码
    @PutMapping("/{student_id}/password")
    public ResponseEntity<?> updatePassword(
            @PathVariable("student_id") Integer student_id,
            @RequestBody Map<String, String> request) {
        String newPassword = request.get("student_password");
        boolean result = studentService.updatePassword(student_id, newPassword);
        if (result) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.status(500).body("密码修改失败");
        }
    }
    // 根据ID查询学生
    //http://localhost:8080/student/#{student_id}
    @ApiOperation("根据ID查询学生")
    @GetMapping("/{student_id}")
    public Student getStudentById(@PathVariable("student_id") Integer studentId) {
        return studentService.getStudentById(studentId);
    }

    // 根据学生ID更新学生信息
    //http://localhost:8080/student/updateAll/{student_id}
    @ApiOperation("根据学生ID更新学生信息")
    @PutMapping("/updateAll/{student_id}")
    public ResponseEntity<String> updateAll(
            @PathVariable Integer student_id,
            @RequestBody Student student) {
        student.setStudent_id(student_id);
        boolean success = studentService.updateStudent(student);
        return success ?
                ResponseEntity.ok("学生信息更新成功") :
                ResponseEntity.badRequest().body("学生信息更新失败");
    }

    //新增学生信息
    //http://localhost:8080/student/insert
    @PostMapping("/insert")
    public ResponseEntity<Integer> addStudent(@RequestBody Student student) {
        // 判空
        if (student.getStudent_id() == null || student.getStudent_id().toString().trim().isEmpty()) {
            return ResponseEntity.ok(-1); // 学号不能为空
        }
        // 判断学号是否已存在
        Student exist = studentService.getStudentById(student.getStudent_id());
        if (exist != null) {
            return ResponseEntity.ok(0); // 学号已存在
        }
        boolean success = studentService.addStudent(student);
        return ResponseEntity.ok(success ? 1 : -2); // 1成功，-2失败
    }

    //学生信息详情表单
    //http://localhost:8080/view/{student_id}
    @ApiOperation("根据ID查询学生多表详情")
    @GetMapping("/view/{student_id}")
    public ViewStudent getViewStudentById(@PathVariable("student_id") Integer studentId) {
        return studentService.getViewStudentById(studentId);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<Integer> deleteStudent(@RequestParam Integer student_id) {
        boolean success = studentService.logicDeleteStudent(student_id);
        return ResponseEntity.ok(success ? 1 : 0);
    }
    @PostMapping("/deleteBatch")
    public ResponseEntity<Integer> deleteBatch(@RequestBody List<Integer> ids) {
        boolean success = studentService.logicDeleteStudents(ids);
        return ResponseEntity.ok(success ? 1 : 0);
    }

    @GetMapping("/getTotalStudentsNum")
    public int getTotalStudentsNum() {
        return studentService.getTotalStudentsNum();
    }

    //登录用
    //根据学生电话查询学生信息
    @GetMapping("/stuLoginByPhone")
    public Student findStudentByPhone(@RequestParam("student_phone") String student_phone) {
        return studentService.findStudentByPhone(student_phone);
    }

    // 根据项目ID查找所有相关学生
    @GetMapping("/byProject")
    public List<Student> getStudentsByProject(@RequestParam Integer projectId) {
        return studentService.getStudentsByProjectId(projectId);
    }
}